module(...,package.seeall)

local json = require "json"


function postHighScore(score)
	print("postHighScore")
	local facebook = require "facebook"
	local facebookScore = score
	print("Facebook score "..facebookScore)

	-- listener for "fbconnect" events
	local function listener( event )
    	print("Call Back listener "..event.type)
    	if ( "session" == event.type ) then
        	-- upon successful login, request list of friends of the signed in user
        	if ( "login" == event.phase ) then
            	
            	theMessage = "Beat my score ".. facebookScore
            	
            	facebook.request( "me/feed", "POST", {
				message=theMessage,
				name="Do you think you can beat my score?",
				caption="Play Balloons and Jewels now to find out!",
				link="https://play.google.com/store/apps/details?id=com.venturus.balloons&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS52ZW50dXJ1cy5iYWxsb29ucyJd",
				picture="https://sites.google.com/site/balloonsjewels/home/icon512.png?attredirects=0" } )

            	-- Fetch access token for use in Facebook's API
            	local access_token = event.token
            	print( access_token )
        	end
    	elseif ( "request" == event.type ) then
        	-- event.response is a JSON object from the FB server
        	local response = event.response

        	-- if request succeeds, create a scrolling list of friend names
        	if ( not event.isError ) then
            	response = json.decode( event.response )
				--print(response.data)
            	--[[
            	local data = response.data
            	for i=1,#data do
                	local name = data[i].name
                	print( name )
            	end
            	]]--
        	end
    	elseif ( "dialog" == event.type ) then
        	print( "dialog", event.response )
    	end
	end

	-- NOTE: You must provide a valid application id provided from Facebook
	local appId = "420122871357861"
	if ( appId ) then
        facebook.login( appId, listener, {"publish_stream"} )
	else
        
    local function onComplete( event )
    	system.openURL( "http://developers.facebook.com/setup" )
    end

    native.showAlert( "Error", "To develop for Facebook Connect, you need to get an application id from Facebook's website.", { "Learn More" }, onComplete )
end


end

